package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.TestBean;

/**
 * 补卡审批列表适配器
 */
public class MakeUpApprovalAdapter extends AppAdapter<TestBean> {

    private OnButtonClickListener mButtonClickListener;

    public MakeUpApprovalAdapter(Context context) {
        super(context);
    }

    /**
     * 设置按钮点击监听
     */
    public void setOnButtonClickListener(OnButtonClickListener listener) {
        mButtonClickListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder();
    }

    private final class ItemViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvTitle;
        private final TextView tvStatus;
        private final TextView tvApplicantValue;
        private final TextView tvApplyTimeValue;
        private final TextView tvReasonValue;
        private final TextView tvClockInTimeValue;
        private final LinearLayout llBottomButtons;
        private final TextView tvRejectInitiator;
        private final TextView tvRejectPrevious;
        private final TextView tvApprove;

        private ItemViewHolder() {
            super(R.layout.make_up_approval_item);
            tvTitle = findViewById(R.id.tv_title);
            tvStatus = findViewById(R.id.tv_status);
            tvApplicantValue = findViewById(R.id.tv_applicant_value);
            tvApplyTimeValue = findViewById(R.id.tv_apply_time_value);
            tvReasonValue = findViewById(R.id.tv_reason_value);
            tvClockInTimeValue = findViewById(R.id.tv_clock_in_time_value);
            llBottomButtons = findViewById(R.id.ll_bottom_buttons);
            tvRejectInitiator = findViewById(R.id.tv_reject_initiator);
            tvRejectPrevious = findViewById(R.id.tv_reject_previous);
            tvApprove = findViewById(R.id.tv_approve);

            // 设置按钮点击事件
            tvRejectInitiator.setOnClickListener(v -> {
                if (mButtonClickListener != null) {
                    int position = getViewHolderPosition();
                    TestBean item = getItem(position);
                    mButtonClickListener.onRejectToInitiator(position, item);
                }
            });

            tvRejectPrevious.setOnClickListener(v -> {
                if (mButtonClickListener != null) {
                    int position = getViewHolderPosition();
                    TestBean item = getItem(position);
                    mButtonClickListener.onRejectToPrevious(position, item);
                }
            });

            tvApprove.setOnClickListener(v -> {
                if (mButtonClickListener != null) {
                    int position = getViewHolderPosition();
                    TestBean item = getItem(position);
                    mButtonClickListener.onApprove(position, item);
                }
            });
        }

        @Override
        public void onBindView(int position) {
            TestBean item = getItem(position);

            // 标题固定为"补卡"
            tvTitle.setText(item.getName());

            // 根据type设置状态标签和底部按钮显示
            String type = item.getType();
            switch (type) {
                case "1":
                    // 待审批
                    tvStatus.setText("待审批");
                    tvStatus.setTextColor(getColor(R.color.red_app));
                    tvStatus.setBackgroundResource(R.drawable.shape_label_red);
                    llBottomButtons.setVisibility(View.VISIBLE);
                    break;
                case "2":
                    // 已通过
                    tvStatus.setText("已通过");
                    tvStatus.setTextColor(getColor(R.color.green_label));
                    tvStatus.setBackgroundResource(R.drawable.shape_label_green);
                    llBottomButtons.setVisibility(View.GONE);
                    break;
                case "3":
                    // 已驳回
                    tvStatus.setText("已驳回");
                    tvStatus.setTextColor(getColor(R.color.orange_01));
                    tvStatus.setBackgroundResource(R.drawable.shape_label_orange);
                    llBottomButtons.setVisibility(View.GONE);
                    break;
            }

            // 模拟数据
            tvApplicantValue.setText("陈文函");
            tvApplyTimeValue.setText("2025-8-18");
            tvReasonValue.setText("该区域为补卡事由内容");
            tvClockInTimeValue.setText("2025-08-27 周三07:00");
        }
    }

    /**
     * 按钮点击监听接口
     */
    public interface OnButtonClickListener {
        /**
         * 驳回发起人
         */
        void onRejectToInitiator(int position, TestBean item);

        /**
         * 驳回上一级
         */
        void onRejectToPrevious(int position, TestBean item);

        /**
         * 审核通过
         */
        void onApprove(int position, TestBean item);
    }
}
